//
//  GYCartViewModel.m
//  GYDemo
//
//  Created by mac on 2018/4/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYCartViewModel.h"
#import "Head.h"

@implementation GYCartShopModel
@end

@implementation GYCartViewModel
#pragma mark - getter and setter
-(NSMutableArray *)datas
{
    if (!_datas) {
        _datas = [NSMutableArray new];
    }
    return _datas;
}
-(BOOL)is_selected
{
    NSInteger count = 0;
    for (GYCartShopModel *model in self.datas) {
        if (model.is_selected) {
            count += 1;
        }
    }
    if (count == self.datas.count) {
        return YES;
    }else{
        return NO;
    }
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        [[InterfaceManager shareInterface].specialInterfaces addObject:@"UVTGetCartList"];
    }
    return self;
}
#pragma mark - method
+(instancetype)shareInterface
{
    static GYCartViewModel *manager;
    kDISPATCH_ONCE_BLOCK(^(){
        if (!manager) {
            manager = [GYCartViewModel new];
        }
    });
    return manager;
}

-(NSString *)finalPrice
{
    CGFloat price = 0;
    for (GYCartShopModel *model in self.datas) {
        if (model.is_selected) {
            price += model.price.floatValue;
        }
    }
    return [NSString stringWithFormat:@"合计：%.2f元",price];
}

-(NSString *)shopcarids
{
    NSMutableArray *arr = [NSMutableArray new];
    for (GYCartShopModel *model in self.datas) {
        if (model.is_selected) {
            NSString *ids = [NSString stringWithFormat:@"%ld",model.shopcarid];
            [arr addObject:ids];
        }
    }
    NSString *res = @"";
    res = [arr componentsJoinedByString:@","];
    return res;
}

-(void)chooseCart:(GYCartShopModel *)cart;
{
    BOOL issel = cart.is_selected;
    cart.is_selected = !issel;
    NSMutableArray *datas = self.datas;
    [self setDatas:datas];
}

-(void)chooseAll
{
    BOOL issel = self.is_selected;
    for (GYCartShopModel *model in self.datas) {
        model.is_selected = !issel;
    }
    NSMutableArray *datas = self.datas;
    [self setDatas:datas];
}

-(void)requestDatas
{
    @WeakSelf;
    NSString *interface = @"UVTGetCartList";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:nil handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        if (0 == infoModel.status) {
            NSArray *data =(NSArray*)info;
            if ([data isKindOfClass:[NSArray class]]) {
                NSMutableArray *arr = [NSMutableArray new];
                for (NSDictionary *dic in data) {
                    GYCartShopModel *model = [GYCartShopModel yy_modelWithJSON:dic];
                    model.is_selected = YES;
                    [arr addObject:model];
                }
                [selfp setDatas:arr];
            }
            
        }else{
            //            [FTIndicator showToastMessage:infoModel.message];
            NSMutableArray *arr = [NSMutableArray new];
            [selfp setDatas:arr];
        }
    }];
}
-(void)requestChange:(GYCartShopModel *)model num:(NSString *)value
{
//    @WeakSelf;
//    NSMutableDictionary *para = [NSMutableDictionary new];
//    NSString *idd = [NSString stringWithFormat:@"%ld",model.shopcarid];
//    [para setObject:idd forKey:@"id"];
//    [para setObject:value forKey:@"num"];
//    NSString *interface = @"modifyShoppingCar";
//    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
//        [selfp requestDatas];
//    }];
}
-(void)requestAdd:(NSMutableDictionary *)para
{
    @WeakSelf;
    NSString *interface = @"UVTaddWineCart";
    [[InterfaceManager shareInterface]requetInterface:interface withParameter:para handler:^(NSDictionary *info, InterfaceStatusModel *infoModel) {
        [FTIndicator showSuccessWithMessage:@"成功加入购物车"];
        [selfp requestDatas];
    }];
}
@end
